package csu.web.jpetstore.web.servlet;

import csu.web.jpetstore.domain.Item;
import csu.web.jpetstore.domain.Product;
import csu.web.jpetstore.persistence.impl.LoggerDAOImpl;
import csu.web.jpetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import static csu.web.jpetstore.persistence.impl.LoggerDAOImpl.loginLogger;

public class ProductViewServlet extends HttpServlet {
    private CatalogService catalogService;

    private static final String PRODUCT_VIEW = "/WEB-INF/jsp/catalog/product.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String productId = req.getParameter("productId");
        LoggerDAOImpl loggerDAO=new LoggerDAOImpl();
        loginLogger.setStarttime();
        loginLogger.setAction(loginLogger.getUsername()+"选择"+productId);
        try {
            loggerDAO.logger(loginLogger);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        catalogService = new CatalogService();
       Product product = catalogService.getProduct(productId);
       List<Item> itemList = catalogService.getItemListByProduct(productId);
       HttpSession session = req.getSession();
       session.setAttribute("product", product);
       session.setAttribute("itemList", itemList);
       req.getRequestDispatcher(PRODUCT_VIEW).forward(req, resp);
    }
}
